﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using BLL.Workers;
using BLL.Workflows;
using DO.Workflows;
using DO.Clustering;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test.Workflows
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class WorkflowDesignBLLTest
    {
        public WorkflowDesignBLLTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestSaveWorkflowDesign()
        {
            try
            {
                WorkflowDesign wfDesign = new WorkflowDesign();
                wfDesign.Author = Environment.UserName;
                wfDesign.CreatedOn = DateTime.Now;
                wfDesign.FileName = "New workflow.wox";
                wfDesign.ModifiedOn = DateTime.Now;
                wfDesign.Version = new Version(1, 0, 0, 0);
                wfDesign.WorkflowID = Guid.NewGuid();
                wfDesign.WorkflowName = "New workflow";
                wfDesign.Components = new Dictionary<Guid, WorkflowComponentBase>();
                wfDesign.SuccessConnectors = new Dictionary<Guid, List<Guid>>();
                wfDesign.FailureConnectors = new Dictionary<Guid, List<Guid>>();
                ManualStart manualStart=new ManualStart();
                wfDesign.Components.Add(manualStart.ComponentID, manualStart);
                DocumentClusteringComponent cluster=new DocumentClusteringComponent();
                cluster.Dependencies.Add(manualStart.ComponentID);
                wfDesign.Components.Add(cluster.ComponentID, cluster);
                EmailComponent email=new EmailComponent();
                email.Dependencies.Add(cluster.ComponentID);
                wfDesign.Components.Add(email.ComponentID, email);
                List<Guid> toComponentIDs=new List<Guid>();
                toComponentIDs.Add(cluster.ComponentID);
                wfDesign.SuccessConnectors.Add(manualStart.ComponentID, toComponentIDs);
                WorkflowManager.SaveWorkflow(wfDesign);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void TestGetWorkflowDesign()
        {
            try
            {
                string workflowName = "New workflow";
                WorkflowDesign wfDesign = WorkflowManager.GetWorkflowByName(workflowName);
                Assert.IsTrue(wfDesign.WorkflowName==workflowName);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void ListWorkflows()
        {
            try
            {
                List<string> wfNames = WorkflowManager.ListWorkflowNames();
                Assert.IsTrue(wfNames.Count>0);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void TestRunWorkflow()
        {
            try
            {
                WorkflowDesign wfDesign = WorkflowManager.GetWorkflowByName("Cluster");
                WorkflowManualTester.RunWorkflow(wfDesign);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
    }
}
